package com.ruoyi.system.web.controller;

import com.ruoyi.domain.request.EsHouseRequest;
import com.ruoyi.domain.result.Result;
import com.ruoyi.domain.vo.EsHouse;
import com.ruoyi.system.domain.HouseResponse;
import com.ruoyi.system.web.feign.GoToLogin;
import com.ruoyi.system.web.feign.GoToSearchController;
import com.ruoyi.system.web.feign.GoToSearchHtml;
import com.ruoyi.system.web.service.ClientHouseService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

/**
 * @author 姚炜琦
 * @Classname ClientHouseController
 * @Description 经纪人 + 客户 + 客户端
 * @Date 2022/10/19 22:36
 */
@Controller
@RequestMapping("/houseClient")
@Log4j2
public class ClientHouseController {

    @Autowired
    private ClientHouseService clientHouseService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private GoToLogin goToLogin;

    @Autowired
    private GoToSearchController goToSearchController;

    /**
     * 去经纪人 + 客户的主页 + 查询直售房列表 + 查询出租房的列表
     * @param model
     * @return
     */
    @RequestMapping(value = "/index.html")
    public String login(Model model){
//        http://127.0.0.1:6661/houseClient/index.html

        // 查询直售房
        ArrayList<HouseResponse> houseSaleList = clientHouseService.findClientHouseByTypeOne();
        // 查询出租房
        ArrayList<HouseResponse> houseRentalList = clientHouseService.findClientHouseByTypeTwo();

        model.addAttribute("houseSaleList", houseSaleList);
        model.addAttribute("houseRentalList", houseRentalList);
        //存入到缓存当中
        ValueOperations ops = redisTemplate.opsForValue();
        ops.set("houseSaleList",houseSaleList);
        ops.set("houseRentalList",houseRentalList);
        return "index";
    }

    /**
     * 去登录注册页面
     * @param model
     * @return
     */
    @RequestMapping(value = "/gotoLogin")
    @ResponseBody
    public String gotoLogin(Model model){
        return goToLogin.loginAndRegistration(model);
    }

    /**
     * 携带city信息去search服务查询房源信息
     * @param esHouseRequest
     * @return
     */
    @PostMapping("/goToSearch")
    @ResponseBody
    public Result<ArrayList<EsHouse>> goToSearch(@RequestBody EsHouseRequest esHouseRequest){
        return goToSearchController.esHouseList(esHouseRequest);
    }
}
